package com.chen.palmar.project.init;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.chen.palmar.R;
import com.chen.palmar.common.constant.AppConstant;
import com.chen.palmar.common.datasource.DataCenter;
import com.chen.palmar.common.override.HttpSubscriber;
import com.chen.palmar.entity.HomeBanEntity;
import com.primb.androidlibs.net.exception.ApiException;
import com.squareup.picasso.Picasso;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Chen on 2016/11/29.
 * 功能描述：
 */

public class WelcomeActivity extends AppCompatActivity {
    @Bind(R.id.iv_welcome)
    ImageView ivWelcome;
    private Animation alphaAnimation;
    private Intent intent;
    private CompositeSubscription subscription;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        ButterKnife.bind(this);
        init();
    }

    public void init() {
        alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_welcome_activity_alpha);
        alphaAnimation.setFillEnabled(true);
        alphaAnimation.setFillAfter(true);
        ivWelcome.setAnimation(alphaAnimation);
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                intent = new Intent();
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                String userId = DataCenter.httpConfig.getUserId();
                String loginToken = DataCenter.httpConfig.getLoginToken();
                if (!loginToken.equals("soask_5366.com") && !userId.equals("-1")) {
                    intent.setClass(WelcomeActivity.this, MainActivity.class);
                } else {
                    intent.setClass(WelcomeActivity.this, LoginActivity.class);
                }
                startActivity(intent);
                WelcomeActivity.this.finish();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        subscription = new CompositeSubscription();

        try {
//            getBannerData();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 轮播图数据
     */
    private void getBannerData() {
        HashMap<String, String> map = new HashMap<>();
        map.put("type", "1");
        map.put("position", "4");
        Subscription banSub = DataCenter.startImg(map).subscribe(new HttpSubscriber<HomeBanEntity>(this, null) {
            @Override
            public void onNext(HomeBanEntity homeEntity) {
                if (homeEntity.getData().size() > 0) {
                    Picasso.with(WelcomeActivity.this).load(homeEntity.getData().get(0).getImg()).resize(AppConstant.SCREEN_WIDTH, AppConstant.SCREEN_HEIGHT).into(ivWelcome);
                }
            }

            @Override
            public void onError(ApiException e) {
            }
        });
        subscription.add(banSub);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        if (subscription != null) {
            subscription.clear();
        }
    }
}
